package jsyf.oa.web.system;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.ExportExcel;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.system.MilkFacade;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/11/8 0008.
 */
@Api(value = "接口", description = "接口")
@Controller
@RequestMapping("/milk")
public class MilkController {
    @Resource
    private MilkFacade milkFacade;

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String resultId = this.milkFacade.save(jsonData);
        String result = "";
        if ("".equals(resultId)) {
            result = BaseUtils.errorrRespond("服务器错误");
        } else {
            JSONObject obj = new JSONObject();
            obj.put("id", resultId);
            result = BaseUtils.succsessObjRespond(obj);
        }
        ResponseUtils.putJsonResponse(response, result);
    }

    @RequestMapping(value = "/update.json", method = RequestMethod.POST)
    public void update(HttpServletResponse response, @RequestBody String jsonData) {
        String resultId = this.milkFacade.update(jsonData);
        String result = "";
        if ("".equals(resultId)) {
            result = BaseUtils.errorrRespond("服务器错误");
        } else {
            JSONObject obj = new JSONObject();
            obj.put("id", resultId);
            result = BaseUtils.succsessObjRespond(obj);
        }
        ResponseUtils.putJsonResponse(response, result);
    }

    @RequestMapping(value = "/getWxId.json", method = RequestMethod.POST)
    public void getWxId(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = milkFacade.getWxId(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
    }

    @RequestMapping(value = "/getByInformation.json", method = RequestMethod.POST)
    public void getByInformation(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = milkFacade.getByInformation(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
    }
    @RequestMapping(value = "/getByPage.json")
    public void getByPage(HttpServletResponse response) throws Exception {
        List<JSONObject> records = milkFacade.getByCondition();
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrTimeRespond(records));
    }
    /*
    导出问卷调差
   */
    @RequestMapping(value = "/getDownByCondition.json")
    public void getDownByCondition(HttpServletResponse response) throws Exception {
        List<JSONObject> records = milkFacade.getByCondition();
        getDownExcel(records, response);
    }

    public void getDownExcel(List<JSONObject> jsonObjectList, HttpServletResponse response) throws Exception {
        //导出表名、列名
        String title = "问卷调查";
        String[] rowsName = new String[]{"调查时间","联系方式", "门牌号", "建议", "购买状态", "您觉得牛奶是否补充营养？", "鲜奶跟超市卖的常温奶，您会选择哪个？",
                "您喝奶更看重营养还是口味？", "您更相信哪个品牌？","您目前所喝牛奶的价格大概在多少左右？","您家有订奶么？",
                "订的哪个品牌？","订的什么类型的牛奶？","家中主要为谁订奶？","在以下送奶方式中您更倾向于哪一个？",
        "如果您有订奶，您会坚持订奶么？","您是否知道小区巴氏鲜奶吧(8-1-102)？","导致您不订奶的因素是什么？","您对当前订奶的评价？"};
        List<Object[]> dataList = new ArrayList<Object[]>();
        Object[] objs = null;
        for (int i = 0; i < jsonObjectList.size(); i++) {
            JSONObject jsonObject = jsonObjectList.get(i);
            objs = new Object[rowsName.length];
            objs[0] = jsonObject.get("createTime");
            objs[1] = jsonObject.get("information");
            objs[2] = jsonObject.get("doorNumber");
            objs[3] = jsonObject.get("remark");
            objs[4] = "1".equals(jsonObject.get("buyFlag")) ? "已购买" : "未购买";
            objs[5] = jsonObject.get("t1");
            objs[6] = jsonObject.get("t2");
            objs[7] = jsonObject.get("t3");
            objs[8] = jsonObject.get("t4");
            objs[9] = jsonObject.get("t5");
            objs[10] = jsonObject.get("t6");
            objs[11] = jsonObject.get("t7");
            objs[12] = jsonObject.get("t8");
            objs[13] = jsonObject.get("t9");
            objs[14] = jsonObject.get("t10");
            objs[15] = jsonObject.get("t11");
            objs[16] = jsonObject.get("t12");
            objs[17] = jsonObject.get("t13");
            objs[18] = jsonObject.get("t14");
            dataList.add(objs);
        }
        ExportExcel ex = new ExportExcel(title, rowsName, dataList, response);
        ex.export();
    }
}
